/**
 * 打开页面
 * @params path=页面地址  needLogin= 跳转的页面是需要登录吗
 * 
*/
export function openPage (path,needLogin=false) {
	console.log('path',path)
	if(needLogin && !uni.getStorageSync('userToken')) {
		uni.navigateTo({
			url: '/pages/login/index'
		})
		return;
	}
	if (!path || path.length < 4) return;
	uni.navigateTo({
		url: path
	})
}

export function isLogin (needLogin=false) {
	if(!uni.getStorageSync('userToken')) {
		needLogin && uni.navigateTo({url: '/pages/login/index'})
		return false;
	}
	return true;
}


export function getPayData(start,end, type, day) {
	day = day?new Date(day).getTime(): new Date().getTime();
	start = new Date(start).getTime();
	end = new Date(end).getTime();
	if(day< start) return type? 1 : "未开始";
	if(day>end) return type? 2 :'已结束';
	return type? 0 : '售票中'
}

export function getDay(day) {
  var today = new Date();
  var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(targetday_milliseconds);
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate  = doHandleMonth(tDate);
  return tYear + "-" + tMonth + "-" + tDate;
}

export function doHandleMonth(month) {
  var m = month;
  if (month.toString().length == 1) {
    m = "0" + month;
  }
  return m;
}

export function getWeek(time) {
	const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
	const date = new Date(time);
	const dayOfWeek = weekday[date.getDay()];
	return dayOfWeek;
}

export function repairRichText(html) {
	// 去掉<img>中style /width / height属性
	if(!html) return;
	let newContent = html.replace(/<img[^>]*>/gi, (match) => {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '')
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '')
		match = match.replace(/style\s*=\s*(["'])(?:(?!\1).)*\1/gi, '') // 去除 style=""这样的属性
		return match
	})
	// 修改所有style里的width属性为max-width:100%
	newContent = newContent.replace(/style="[^"]+"/gi, (match) => {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
			'max-width:100%;')
		return match
	})

	// 去掉所有<br/>标签
	newContent = newContent.replace(/<br[^>]*\/>/gi, '')
	// img标签添加style属性：max-width:100%;height:auto
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
	)
	return newContent;
}

export function getBtnName(item) {
  if ([2, 3].includes(item.productType) && ![0, 1].includes(item.orderStatus)) {
    return "";
  }
  return item.orderStatus == 0 ? "去支付" : item.orderStatus == 1 ? "检票入场" : "查看详情";
}
